package com.dabaozhan.modules.wx_app.controller;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.dabaozhan.common.exception.RRException;
import com.dabaozhan.common.utils.ImageOperation;
import com.dabaozhan.common.utils.ValidationBeanUtils;
import com.dabaozhan.modules.wx_app.base.PageResult;
import com.dabaozhan.modules.wx_app.base.ResponseDto;
import com.dabaozhan.modules.wx_app.entity.SitOutRecourdQueryEntity;
import com.dabaozhan.modules.wx_app.entity.SiteOutBillQueryEntity;
import com.dabaozhan.modules.wx_app.entity.SiteOutRecordEntity;
import com.dabaozhan.modules.wx_app.enums.ResponseCodeEnum;
import com.dabaozhan.modules.wx_app.facade.SiteOutRecordFacade;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

@Api(value = "出库到厂")
@RestController
@RequestMapping("/wx_app")
public class SiteOutRecordController {

	private static final Logger LOGGER = LoggerFactory.getLogger(SiteOutRecordController.class);
	
	@Autowired
	private SiteOutRecordFacade siteOutRecordFacade;

	@ApiOperation(value = "查询已出库到厂数据")
    @RequestMapping(value = "/getOutSite")
	public String getOutSite(SitOutRecourdQueryEntity entity){
		ResponseDto<List<SiteOutRecordEntity>> responseDto = new ResponseDto<List<SiteOutRecordEntity>>();
		try{
			//验证参数是否为空
			ValidationBeanUtils.validateBeanRequestDto(entity);
			PageResult<List<SiteOutRecordEntity>> pageResult = siteOutRecordFacade.getOutRecord(entity);
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
			responseDto.setResult(pageResult.getListData());
			responseDto.setTotal(pageResult.getTotal());
			responseDto.setPage(entity.getPage());
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("查询已出库到厂数据发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("查询已出库到厂数据请求参数:{}-返回结果:{}",JSON.toJSONString(entity),JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}
	
	@ApiOperation(value = "查询销货对账单")
    @RequestMapping(value = "/balanceBill")
	public String balanceBill(SiteOutBillQueryEntity entity){
		ResponseDto<List<SiteOutRecordEntity>> responseDto = new ResponseDto<List<SiteOutRecordEntity>>();
		try{
			//验证参数是否为空
			ValidationBeanUtils.validateBeanRequestDto(entity);
			PageResult<List<SiteOutRecordEntity>> pageResult = siteOutRecordFacade.getBalanceBill(entity);
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
			responseDto.setResult(pageResult.getListData());
			responseDto.setTotal(pageResult.getTotal());
			responseDto.setPage(entity.getPage());
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("查询销货对账单发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("查询销货对账单请求参数:{}-返回结果:{}",JSON.toJSONString(entity),JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}
	
	@ApiOperation(value = "出库登记")
    @RequestMapping(value = "/outSite")
	public String addOutSite(SiteOutRecordEntity entity,HttpServletRequest request){
		ResponseDto<String> responseDto = new ResponseDto<String>();
		try{
			//图片保存路径
			String pathUrl = request.getSession().getServletContext().getRealPath("/")+ ("fileImage/");
			//验证参数是否为空
			ValidationBeanUtils.validateBeanRequestDto(entity);
			String orderSerial = siteOutRecordFacade.addOutRecord(entity,pathUrl);
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
			responseDto.setResult(orderSerial);
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("出库登记发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("出库登记请求参数:{}-返回结果:{}",JSON.toJSONString(entity),JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}
	
	@ApiOperation(value = "到厂登记")
    @RequestMapping(value = "/updateArriveSite")
	public String updateArriveSite(SiteOutRecordEntity entity,HttpServletRequest request){
		ResponseDto<String> responseDto = new ResponseDto<String>();
		try{
			if (!"".equals(entity.getArrivePic())) {
				//图片保存路径
				String pathUrl = request.getSession().getServletContext().getRealPath("/")+ ("fileImage/");
				//获取文件目录图片 并保存到七牛
				String url = ImageOperation.encodeImage(entity.getArrivePic().substring(entity.getArrivePic().lastIndexOf("/") + 1), pathUrl);
				//最终图片路径
				entity.setArrivePic(url);
			}
			siteOutRecordFacade.updateArriveRecord(entity);
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("到厂登记发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("到厂登记请求参数:{}-返回结果:{}",JSON.toJSONString(entity),JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}
	
	@ApiOperation(value = "更新结算状态")
    @RequestMapping(value = "/updateSettleStatus")
	public String updateSettleStatus(SiteOutRecordEntity entity){
		ResponseDto<String> responseDto = new ResponseDto<String>();
		try{
			if(StringUtils.isBlank(entity.getOrderSerial()) || null == entity.getSettleStatus()){
				responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_101);
			}else{
				siteOutRecordFacade.updateSettleStatus(entity);
				responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
			}
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("更新结算状态发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("更新结算状态请求参数:{}-返回结果:{}",JSON.toJSONString(entity),JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}

}
